package org.jeecgframework.core.extend.video;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jeecgframework.core.util.FileUtils;

public class MenCoderConverter implements AVIConverter {
	protected static Log logger = LogFactory.getLog(MenCoderConverter.class);
	/**
	 * mencoder路径
	 */
	private String mencoderHome;
	public MenCoderConverter(){
		
	}
	
	public MenCoderConverter(String mencoderHome){
		this.mencoderHome = mencoderHome;
	}
	public void convert2Avi(String inputFile, String aviFile) {
		List<String> commend = new ArrayList<String>();
		commend.add(mencoderHome);
		commend.add(inputFile);
		commend.add("-o");
		commend.add(aviFile);
		commend.add("-oac");
		commend.add("mp3lame");
		commend.add("-ovc");
		commend.add("lavc");
		try {
			logger.info("视频:"+inputFile+"转化为Avi格式开始...");
			ProcessBuilder builder = new ProcessBuilder(commend);
			builder.redirectErrorStream(true);
			Process process = builder.start();
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String tmp = null;
			while((tmp = br.readLine()) != null){
				logger.info(tmp);
			}
			process.waitFor();
			logger.info("视频:"+inputFile+"转化为Avi格式结束...");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("视频:"+inputFile+",转化为Avi格式失败!",e);
		}
	}

	public void convert2Avi(String inputFile) {
		String aviFile = FileUtils.getFilePrefix(inputFile)+".avi";
		convert2Avi(inputFile, aviFile);
	}

	
	public static void main(String[] args) {
		AVIConverter avic = new MenCoderConverter("E:\\workspace\\self\\LearnOnline/WebRoot/video/mencoder/mencoder.exe");
		avic.convert2Avi("d:mp4.mp4", "d:\\b.avi");
	}
	

}
